package com.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lkd.entity.Business;
import com.lkd.entity.Node;
import com.lkd.entity.VendingMachine;
import com.lkd.entity.VmType;
import com.lkd.mapper.*;
import com.lkd.service.NodeService;
import com.lkd.service.VendingMachineService;
import com.lkd.vo.NodeVo;
import com.lkd.vo.PageBean;
import com.lkd.vo.RegionVo;
import com.lkd.vo.VendingMachineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 聂德祥
 * @since 2024-08-19
 */
@Service
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineMapper, VendingMachine> implements VendingMachineService {
@Autowired
private RegionMapper regionMapper;
@Autowired
private VendingMachineMapper vendingMachineMapper;
@Autowired
private BusinessMapper businessMapper;
@Autowired
private VmTypeMapper vmTypeMapper;
@Autowired
private NodeMapper nodeMapper;
@Autowired
private NodeService nodeService;
    @Override
    public PageBean<VendingMachineVo> getVmByPage(String innerCode, Integer pageIndex, Integer pageSize) {
        Page<VendingMachine> vendingMachinePage = new Page<>(pageIndex, pageSize);
        Page<VendingMachineVo> vendingMachineVoPage = new Page<>(pageIndex, pageSize);
         vendingMachinePage = getBaseMapper().selectPage(vendingMachinePage, new LambdaQueryWrapper<VendingMachine>().like(StrUtil.isNotEmpty(innerCode), VendingMachine::getInnerCode, innerCode));
        if (vendingMachinePage.getRecords().size() > 0) {
            vendingMachineVoPage.setTotal(vendingMachinePage.getTotal());
            vendingMachineVoPage.setSize(vendingMachinePage.getSize());
            vendingMachineVoPage.setCurrent(vendingMachinePage.getCurrent());
            List<VendingMachineVo> vendingMachineVos = vendingMachinePage.getRecords().stream().map(vendingMachine -> {
                VendingMachineVo  vendingMachineVo  = BeanUtil.copyProperties(vendingMachine, VendingMachineVo.class);

//                // 获取区域
//                RegionVo regionVo = regionMapper.getRegionVo(vendingMachine.getRegionId());
//                if (regionVo != null) {
//                    vendingMachineVo.setRegion(regionVo);
//                }
//                // 获取商圈类型
//                Business business = businessMapper.selectById(vendingMachineVo.getBusinessId());
//                if (business != null) {
//                    vendingMachineVo.setBusinessType(business);
//                }
//                // 获取设备数量
//                Long aLong = vendingMachineMapper.selectCount(new LambdaQueryWrapper<VendingMachine>().eq(VendingMachine::getNodeId, vendingMachine.getId()));
//                vendingMachineVo.setVmCount(aLong.intValue());

                // 获取设备类型
                VmType vmType = vmTypeMapper.selectById(vendingMachine.getVmType());
                if(vmType!=null){
                    vendingMachineVo.setType(vmType);
                }
                NodeVo nodeVo = nodeService.findById(vendingMachine.getNodeId());
                if(nodeVo!=null){
                    vendingMachineVo.setNode(nodeVo);
                    if(nodeVo.getRegion()!=null){
                        vendingMachineVo.setRegion(nodeVo.getRegion());
                    }
                }
                return vendingMachineVo;
            }).collect(Collectors.toList());
            if(CollUtil.isNotEmpty(vendingMachineVos)){
                vendingMachineVoPage.setRecords(vendingMachineVos);
            }
        }
        return new PageBean<VendingMachineVo>(vendingMachineVoPage);
    }
}
